package com.sptpc.qimoxiangmu.controller;

import com.sptpc.qimoxiangmu.controller.request.ModelRequest;
import com.sptpc.qimoxiangmu.model.Model;
import com.sptpc.qimoxiangmu.model.vo.ModelVo;
import com.sptpc.qimoxiangmu.repository.ModelRepository;
import com.sptpc.qimoxiangmu.service.TypeService;
import com.sptpc.qimoxiangmu.service.ModelService;
import com.sptpc.qimoxiangmu.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/model")
public class ModelController {
    @Autowired
    private ModelService modelService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private ModelRepository modelRepository;
//显示查询表单页面，成功,映射
@GetMapping("/list.html")
    public ModelAndView listView(){
        ModelAndView mv =new ModelAndView("model/list");
        List<Model> modelList= modelService.findAllModel();
        mv.addObject("modelList",modelList);
        return mv;
    }
    @GetMapping("/list_view_id")
    public ModelAndView listView(String id){
        ModelAndView mv =new ModelAndView("model/list");
        List<Model> modelList= modelService.findModelByTypeId(Long.parseLong(id));//强转String转Long
        mv.addObject("modelList",modelList);
        return mv;
    }
    @GetMapping("/edit.html")
    public ModelAndView editView(String id){
        ModelAndView mv=new ModelAndView("model/edit");
        Model model=modelService.findModelById(Long.parseLong(id));
        mv.addObject("model",model);
        mv.addObject("typeList",typeService.findAll());
        return mv;
    }

    @GetMapping("/insert.html")
    public ModelAndView insertView(){
        ModelAndView mv=new ModelAndView("model/insert");
        mv.addObject("typeList",typeService.findAll());
        return mv;
    }
    //删除
//    @GetMapping("/delete")//成功
//    @ResponseBody
//    public ServerResponse delete(@RequestParam(name="id") Long id){
//        Model drink=modelService.findDrinkById(id);
//        if(drink!=null){
//            modelService.deleteDrink(id);
//            return ServerResponse.createBySuccessMessage("删除成功！");
//        }else {
//            return ServerResponse.createByErrorMessage("找不到删除分类！");
//        }
//    }

    @PostMapping(value = "/save", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ResponseBody
    public ServerResponse save(@Valid ModelRequest modelRequest, BindingResult result) {
        if (result.hasErrors()) {
            return ServerResponse.createByErrorMessage("检验参数有问题");
        }
        Model model = modelService.save(modelRequest);
        if (model != null) {
            return ServerResponse.createBySuccess(model);
        }
        return ServerResponse.createByErrorMessage("找不到此分类或此分类不存在");
    }


    //注册方法json,成功
    @PostMapping(value = "/save", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ServerResponse saveJson(@RequestBody ModelRequest modelRequest) {
        Model model = modelService.save(modelRequest);
        if (model != null) {
            return ServerResponse.createBySuccess(model);
        }
        return ServerResponse.createByErrorMessage("找不到此分类或者没有此分类");
    }
    //得到drink中所有信息，成功
    @GetMapping(value = "/")
    @ResponseBody
    public ServerResponse list(){
        return  ServerResponse.createBySuccess(modelService.findAllModel());
    }
    //成功
    @PostMapping(value = "/",params = "type_name")
    @ResponseBody
    public ServerResponse list(@RequestParam String type_name){
        List<Model> drinkList=modelService.findModelByTypeName(type_name);//findDrinksByCategoryName
        if(drinkList !=null){
            return  ServerResponse.createBySuccess(drinkList);
        }else {
            return ServerResponse.createByErrorMessage("没有"+type_name+"的分类");
        }
    }
    //成功
    @PostMapping(value = "/",params = "type_id")
    @ResponseBody
    public ServerResponse list(@RequestParam long type_id){
        List<Model> drinkList=modelService.findModelByTypeId(type_id);
        if(drinkList !=null){
            return  ServerResponse.createBySuccess(drinkList);
        }else {
            return ServerResponse.createByErrorMessage("没有"+type_id+"的分类");
        }
    }


//    更新model信息，成功
    @PostMapping(value = "/update",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ResponseBody
    public ServerResponse update(ModelVo modelVo){
        Model model=modelService.update(modelVo);
        if(model!=null){
            return ServerResponse.createBySuccess(model);
        }
        return ServerResponse.createByErrorMessage("找不到此分类或者没有此分类");
    }
}
